package cn.pfz.controller;

import cn.pfz.Service.IBusinessService;
import cn.pfz.Utils.CharUtil;
import cn.pfz.domain.*;
import cn.pfz.query.Businessquery;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.UUID;

@Controller
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") Businessquery qo) {
        PageInfo<Business> list = businessService.list(qo);

        model.addAttribute("result", list);

        return "business/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {

        if (id == null) {
            return "business/input";
        } else {
            Business business = businessService.selectByPrimaryKey(id);
            model.addAttribute("currbus", business);
            return "business/input";
        }

    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        businessService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate( Business business, HttpServletRequest request,MultipartFile file) throws Exception {
        System.out.println(business);
        if (business.getId() != null) {
            String img = business.getLicense_img();
            String upload = request.getServletContext().getRealPath("/");
            File file1 = new File(upload + img);
            file1.delete();
        }
//处理文件上传

        String originalFilename = file.getOriginalFilename();
//文件名
        String[] split = originalFilename.split("\\.");
        String ext = split[split.length - 1];

        String s = UUID.randomUUID().toString();

        String filename = s + '.' + ext;
        String realPath = request.getServletContext().getRealPath("/img/");
        File file1 = new File(realPath + filename);
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        InputStream inputStream = file.getInputStream();
        //读取数据格式
        byte[] buf = new byte[100];
        int len = 0;
        while ((len = inputStream.read(buf)) != -1) {
            fileOutputStream.write(buf, 0, len);
        }
        fileOutputStream.close();
        //保存数据库
        business.setLicense_img("/img/" + filename);
        //处理乱码
       // CharUtil.convert(business, Business.class);
        //持久化对象

        if (business.getId() == null) {
            businessService.insert(business);
        } else {
            businessService.updateByPrimaryKey(business);
        }
        return "redirect:/business/list";
    }


}
